local Lua__Barrier = fk.CreateSkill {
    name = "Lua__Barrier",
    tags = {Skill.Permanent}
}


Fk:loadTranslationTable{
  ["Lua__Barrier"] = "Barrier",
  [":Lua__Barrier"] = "持恒技，共鸣技，当你即将受到伤害时/体力流失时/体力上限流失时，你可以弃置场上一种花色的牌，视为免疫该伤害/体力流失/体力上限流失。",
  ["#Lua__Barrier-choose"] = "请选择一种花色的牌",
}



Lua__Barrier:addEffect(fk.DamageInflicted, {
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        local can = false
        for _, p in ipairs(room.alive_players) do 
            if #p:getCardIds("ej") > 0 then
                can = true
                break
            end
        end
      return player:hasSkill(Lua__Barrier.name) and target == player and (player.general == "DIEND" or player.deputyGeneral == "DIEND")and can
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local choices = {"Cancel"}
      for _, p in ipairs(room.alive_players) do 
        for _, card in ipairs(p:getCardIds("ej")) do 
            if Fk:getCardById(card,true).suit == Card.Heart then
                if not table.contains(choices, "heart") then
                    table.insert(choices, "heart")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Spade then
                if not table.contains(choices, "spade") then
                    table.insert(choices, "spade")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Club then
                if not table.contains(choices, "club") then
                    table.insert(choices, "club")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Diamond then
                if not table.contains(choices, "diamond") then
                    table.insert(choices, "diamond")
                end
            end
        end
    end
       local choice = player.room:askToChoice(player, {
            choices = choices,
            skill_name = Lua__Barrier.name,
            prompt = "#Lua__Barrier-choose",
        })
        if choice ~= "Cancel" then
            event:setCostData(self, {choice = choice} )
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local choice = event:getCostData(self).choice
      for _, p in ipairs(room.alive_players) do 
        for _, card in ipairs(p:getCardIds("ej")) do 
            if Fk:getCardById(card,true):getSuitString() == choice then
                room:throwCard(card, Lua__Barrier.name, p, player)
            end
        end
      end
      data:preventDamage()
    end
  })
  
  Lua__Barrier:addEffect(fk.PreHpLost, {
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        local can = false
        for _, p in ipairs(room.alive_players) do 
            if #p:getCardIds("ej") > 0 then
                can = true
                break
            end
        end
      return player:hasSkill(Lua__Barrier.name) and target == player and (player.general == "DIEND" or player.deputyGeneral == "DIEND") and can
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local choices = {"Cancel"}
      for _, p in ipairs(room.alive_players) do 
        for _, card in ipairs(p:getCardIds("ej")) do 
            if Fk:getCardById(card,true).suit == Card.Heart then
                if not table.contains(choices, "heart") then
                    table.insert(choices, "heart")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Spade then
                if not table.contains(choices, "spade") then
                    table.insert(choices, "spade")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Club then
                if not table.contains(choices, "club") then
                    table.insert(choices, "club")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Diamond then
                if not table.contains(choices, "diamond") then
                    table.insert(choices, "diamond")
                end
            end
        end
    end
       local choice = player.room:askToChoice(player, {
            choices = choices,
            skill_name = Lua__Barrier.name,
            prompt = "#Lua__Barrier-choose",
        })
        if choice ~= "Cancel" then
            event:setCostData(self, {choice = choice} )
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local choice = event:getCostData(self).choice
      for _, p in ipairs(room.alive_players) do 
        for _, card in ipairs(p:getCardIds("ej")) do 
            if Fk:getCardById(card,true):getSuitString() == choice then
                room:throwCard(card, Lua__Barrier.name, p, player)
            end
        end
      end
      data:preventHpLost()
    end
  })
  
  Lua__Barrier:addEffect(fk.BeforeMaxHpChanged, {
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        local can = false
        for _, p in ipairs(room.alive_players) do 
            if #p:getCardIds("ej") > 0 then
                can = true
                break
            end
        end
        if player:hasSkill(Lua__Barrier.name) and target == player and (player.general == "DIEND" or player.deputyGeneral == "DIEND") then
            return can and data.num < 0
        end
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local choices = {"Cancel"}
      for _, p in ipairs(room.alive_players) do 
        for _, card in ipairs(p:getCardIds("ej")) do 
            if Fk:getCardById(card,true).suit == Card.Heart then
                if not table.contains(choices, "heart") then
                    table.insert(choices, "heart")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Spade then
                if not table.contains(choices, "spade") then
                    table.insert(choices, "spade")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Club then
                if not table.contains(choices, "club") then
                    table.insert(choices, "club")
                end
            end
            if Fk:getCardById(card,true).suit == Card.Diamond then
                if not table.contains(choices, "diamond") then
                    table.insert(choices, "diamond")
                end
            end
        end
    end
       local choice = player.room:askToChoice(player, {
            choices = choices,
            skill_name = Lua__Barrier.name,
            prompt = "#Lua__Barrier-choose",
        })
        if choice ~= "Cancel" then
            event:setCostData(self, {choice = choice} )
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local choice = event:getCostData(self).choice
      for _, p in ipairs(room.alive_players) do 
        for _, card in ipairs(p:getCardIds("ej")) do 
            if Fk:getCardById(card,true):getSuitString() == choice then
                room:throwCard(card, Lua__Barrier.name, p, player)
            end
        end
      end
      data:preventMaxHpChange()
    end
  })




return Lua__Barrier 